﻿function Range(min, max) {

    if(max < min){
        throw new Error("The maximum value must be higher than minimum value");
        return;
    };

    this.Min = min;
    this.Max = max;

    this.Percent = function(value){

        if(value >= max)
            return 1;
        else if(value <= min)
            return 0;

        return (value - min) / (max - min);
    };

    this.Normalize = function(value, foreignRange){
        return foreignRange.Percent(value) * (max - min) + min;
    };

    this.Random = function () {
        return this.Normalize(Math.random(), new Range(0, 1));
    };

    this.RandomInt = function () {
        if (min == max)
            return max;
        return Math.round(this.Random());
    };
};